$(function () {
    $('#loginBox a').on('click', function () {
        $('#regBox').show()
        $('#loginBox').hide()
    })
    $('#regBox a').on('click', function () {
        $('#loginBox').show()
        $('#regBox').hide()
    })

    let form = layui.form
    form.verify({
        username: [
            /^[a-zA-Z0-9]{1,10}$/,
            '用户名必须1到10位数字和字母'
        ],
        pwd: [
            /^[a-zA-Z0-9]{6,12}$/,
            '用户名必须6到12位数字和字母'
        ],
        rePwd: function (value) {
            let pwd = $('#regBox input[name=password]').val()
            if (pwd !== value) {
                return '两次密码输入不一致  '
            }
        }
    })
        //注册功能
        $('#formReg').on('submit', function (e) {
            e.preventDefault()
            axios({
                method: 'POST',
                url: '/api/reguser',
                data: $(this).serialize()
            }).then(res => {
                if (res.data.status !== 0) {
                    return layer.msg(res.data.message)
                }
                console.log(res);
                layer.msg('恭喜你，注册用户成功！')
                $('#regBox a').click()
                $('#formReg')[0].reset()
            })
        })

        //登录功能
        $('#formLogin').on('submit', function (e) {
            e.preventDefault()
            axios({
                method: 'POST',
                url: "/api/login",
                data: $(this).serialize()
            }).then(({
                data: res
            }) => {
                // console.log(res.data);
                if (res.status !== 0) {
                    return layer.msg(res.message)
                }
                console.log(res);
                layer.msg('恭喜你，登录成功！')
                localStorage.setItem('token', res.token)
                location.href = '/index.html'
            })
        })
})